<?php
require_once(dirname(__FILE__) . "../../readCSVandInsertIntoDB/CSVReadAndInsertIntoDB.php");
require_once(dirname(__FILE__) . "../../configs/configs.php");
/*****************************************************************/
function base64_to_file() {
	
	$bannerNo =@$_REQUEST["bannerNo"];
	$folderPath ='upload/banner_'.$bannerNo."/";
	//$fileName = @$_REQUEST["fileName"];
	$fileExt = @$_REQUEST["fileExt"];
	$fileName = $bannerNo;
	$base64Data = @$_REQUEST["base64Data"];
	
	
    //$newBase = urldecode($base);
	$binary = base64_decode($base64Data);
	$file = fopen($folderPath.$fileName.".".$fileExt, 'wb');
	fwrite($file, $binary);
	fclose($file); 
	
	$tempStr ="Banner #" .$bannerNo." ".$fileExt." image has been uploaded successfully !!!";
	echo $tempStr;
}
//************************************************************************************/
function uploadCSVFile(){
	
	$folderPath ='upload/csv/';	
	$fileExt = @$_REQUEST["fileExt"];
	//$fileName =@$_REQUEST["fileName"];
	$fileName = "sampleData";
	$base64Data = @$_REQUEST["base64Data"];
		   
	$binary = base64_decode($base64Data);
	$file = fopen($folderPath.$fileName.".".$fileExt, 'wb');
	fwrite($file, $binary);
	fclose($file); 	
	
	global $conn;	
	if ($conn === false)
	{
		echo "ERROR.Failed to connect to MySQL:" . mysqli_connect_error();
	} 
	else{
		read_CSV_and_insert_into_DB();
	}
		
}
//************************************************************************************/
$ret = "";
switch (@$_REQUEST["method"])
{		
    case "base64_to_file":
        $ret = base64_to_file();
        break;	
	case "uploadCSVFile":
        $ret = uploadCSVFile();
        break;
}

/*******************************************************************************************************************************/
echo $ret;
die();

?>